﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;

namespace FloodFree.Phone
{
    public partial class DetailsPage : PhoneApplicationPage
    {
        // Constructor
        public DetailsPage()
        {
            InitializeComponent();
            DataContext = App.DetailsViewModel;
        }

        // When page is navigated to set data context to selected item in list
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            string selectedIndex = "";
            if (NavigationContext.QueryString.TryGetValue("selectedIndex", out selectedIndex))
            {
                int index = int.Parse(selectedIndex);
                App.DetailsViewModel.Sensor = App.SensorViewModel.Items[index];


                

                double latitude = Double.Parse(App.DetailsViewModel.Sensor.Latitude);
                double longitude = Double.Parse(App.DetailsViewModel.Sensor.Longitude);
                GeoCoordinate coordinate = new GeoCoordinate(latitude,longitude);
                SolidColorBrush color = new SolidColorBrush(Colors.Green);

                if (App.DetailsViewModel.Sensor.LastStatus.Equals("1"))
                {
                    color = new SolidColorBrush(Colors.Red);
                }

                int zoom = 18;

                Pushpin pin = new Pushpin();
                pin.Background = color;
                pin.Location = coordinate;
                pin.Content = App.DetailsViewModel.Sensor.Address;
                map.Children.Add(pin);

                map.Mode = new Microsoft.Phone.Controls.Maps.AerialMode();
                map.SetView(coordinate, zoom);
                
            }

            string cityName = "";
            if (NavigationContext.QueryString.TryGetValue("cityName", out cityName))
            {
                App.DetailsViewModel.CityName = cityName;
            }
        }

        private void btSubcribe_Click(object sender, RoutedEventArgs e)
        {
            if (!App.LoginViewModel.isAuthenticated)
            {
                MessageBox.Show("Você precisa estar logado para receber notificações");
                NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
                return;
            }


        }

    }
}